package com.lyf.shop.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.lyf.common.core.utils.StringUtils;
import com.lyf.common.security.utils.SecurityUtils;
import com.lyf.shop.domain.Employee;
import com.lyf.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lyf.common.log.annotation.Log;
import com.lyf.common.log.enums.BusinessType;
import com.lyf.common.security.annotation.RequiresPermissions;
import com.lyf.shop.domain.Classification;
import com.lyf.shop.service.IClassificationService;
import com.lyf.common.core.web.controller.BaseController;
import com.lyf.common.core.web.domain.AjaxResult;
import com.lyf.common.core.utils.poi.ExcelUtil;
import com.lyf.common.core.web.page.TableDataInfo;

/**
 * 商品分类Controller
 * 
 * @author lyf
 * @date 2023-02-10
 */
@RestController
@RequestMapping("/classification")
public class ClassificationController extends BaseController
{
    @Autowired
    private IClassificationService classificationService;

    /**
     * 查询商品分类列表
     */
    @RequiresPermissions("supermarket:classification:list")
    @GetMapping("/list")
    public TableDataInfo list(Classification classification)
    {
        startPage();
        List<Classification> list = classificationService.selectClassificationList(classification);
        return getDataTable(list);
    }

    /**
     * 查询商品分类列表
     */
    @RequiresPermissions("supermarket:classification:list")
    @GetMapping("/listName")
    public AjaxResult listName(Classification classification)
    {
        classification.setStatus("0");
        List<Classification> list = classificationService.selectClassificationList(classification);
        return AjaxResult.success(list);
    }
    /**
     * 导出商品分类列表
     */
    @RequiresPermissions("supermarket:classification:export")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Classification classification)
    {
        List<Classification> list = classificationService.selectClassificationList(classification);
        ExcelUtil<Classification> util = new ExcelUtil<Classification>(Classification.class);
        util.exportExcel(response, list, "商品分类数据");
    }

    /**
     * 获取商品分类详细信息
     */
    @RequiresPermissions("supermarket:classification:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(classificationService.selectClassificationById(id));
    }

    /**
     * 状态修改
     */
    @RequiresPermissions("supermarket:classification:edit")
    @Log(title = "商品分类状态管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Classification classification)
    {
        classification.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(classificationService.updateClassification(classification));
    }

    /**
     * 新增商品分类
     */
    @RequiresPermissions("supermarket:classification:add")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Classification classification)
    {
        return toAjax(classificationService.insertClassification(classification));
    }

    /**
     * 修改商品分类
     */
    @RequiresPermissions("supermarket:classification:edit")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Classification classification)
    {
        return toAjax(classificationService.updateClassification(classification));
    }

    /**
     * 删除商品分类
     */
    @RequiresPermissions("supermarket:classification:remove")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(classificationService.deleteClassificationByIds(ids));
    }


    @RequiresPermissions("supermarket:classification:query")
    @GetMapping(value = "/classificationName/{userId}")
    public AjaxResult getClassificationNameByUserId(@PathVariable("userId") Long userId){
        if (StringUtils.isNotNull(userId)){
            List<Classification> classificationList = classificationService.getClassificationNameByUserId(userId);
            return AjaxResult.success(classificationList);
        }
        return AjaxResult.error("用户id不能为空");
    }
}
